﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Common;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using MesProjectWinform.Public.Common;
using MesProjectWinform.用户控件;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.Packing
{
    public partial class PackingForm : DevExpress.XtraEditors.XtraForm
    {
        public PackingForm()
        {
            InitializeComponent();
        }
        public async Task ReInit()
        {
            // 设置每页显示记录数选项
            paginationControl1.PageSizeOptions = new List<int> { 1, 10, 20, 50, 100 };

            // 设置默认每页显示记录数
            paginationControl1.PageSize = 10;

            // 设置当前页码
            paginationControl1.CurrentPage = 1;
            var packingList = await GetPackingListAsync();
            gridControl1.DataSource = packingList;
            var gridView = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (gridView != null)
            {
                gridView.Columns.Clear();
                // 添加多选框列
                var checkEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
                gridControl1.RepositoryItems.Add(checkEdit);
                var colSelect = gridView.Columns.AddVisible("IsSelected", "选择");
                colSelect.ColumnEdit = checkEdit;
                colSelect.VisibleIndex = 0; // 设置为第一列
                colSelect.Width = 40;
                //设置多选框默认不选中
                checkEdit.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
                // 其他数据列
                gridView.Columns.AddVisible("PACKINGCODE", "编码");
                gridView.Columns.AddVisible("PACKINGNAME", "名称");
                gridView.Columns.AddVisible("PACKINGTYPE", "混包类型");
                gridView.Columns.AddVisible("INNERBOXLENGTH", "内箱长度(mm)");
                gridView.Columns.AddVisible("INNERBOXWIDTH", "内箱宽度(mm)");
                gridView.Columns.AddVisible("INNERBOXHEIGHT", "内箱高度(mm)");
                gridView.Columns.AddVisible("INNERBOXWEIGHTMIN", "内箱重量最小值(mm)");
                gridView.Columns.AddVisible("INNERBOXWEIGHTMAX", "内箱重量最大值(mm)");
                gridView.Columns.AddVisible("INNERBOXCAPACITY", "内箱容量(mm)");
                gridView.Columns.AddVisible("OUTERBOXLENGTH", "外箱长度(mm)");
                gridView.Columns.AddVisible("OUTERBOXWIDTH", "外箱宽度(mm)");
                gridView.Columns.AddVisible("OUTERBOXHEIGHT", "外箱高度(mm)");
                gridView.Columns.AddVisible("OUTERBOXWEIGHTMIN", "外箱重量最小值(mm)");
                gridView.Columns.AddVisible("OUTERBOXWEIGHTMAX", "外箱重量最大值(mm)");
                gridView.Columns.AddVisible("OUTERBOXCAPACITY", "外箱容量(mm)");
                gridView.Columns.AddVisible("PALLETLENGTH", "栈板长度(mm)");
                gridView.Columns.AddVisible("PALLETWIDTH", "栈板宽度(mm)");
                gridView.Columns.AddVisible("PALLETHEIGHT", "栈板高度(mm)");
                gridView.Columns.AddVisible("PALLETWEIGHTMIN", "栈板重量最小值(mm)");
                gridView.Columns.AddVisible("PALLETWEIGHTMAX", "栈板重量最大值(mm)");
                gridView.Columns.AddVisible("PALLETCAPACITY", "栈板容量(mm)");
                gridView.Columns.AddVisible("PACKINGDESCRIBE", "包装描述");
                gridView.Columns.AddVisible("CREATEUSERNAME", "创建人");
                gridView.Columns.AddVisible("CREATETIME", "创建时间");
                gridView.Columns.AddVisible("UPDATETIME", "更新时间");

                // 设置GridView为可编辑
                gridView.OptionsBehavior.Editable = true;
                // 只允许IsSelected列可编辑，其他列不可编辑
                foreach (DevExpress.XtraGrid.Columns.GridColumn col in gridView.Columns)
                {
                    if (col.FieldName == "IsSelected")
                    {
                        col.OptionsColumn.AllowEdit = true;
                        col.OptionsColumn.AllowFocus = true;
                    }
                    else
                    {
                        col.OptionsColumn.AllowEdit = false;
                        col.OptionsColumn.AllowFocus = false;
                    }
                }
            }
        }

        private async void PackingForm_Load(object sender, EventArgs e)
        {
            // 按钮美化
            simpleButton1.Appearance.Font = new Font("微软雅黑", 10, FontStyle.Bold);
            simpleButton1.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.UltraFlat;
            simpleButton1.Appearance.Options.UseFont = true;
            simpleButton1.Appearance.Options.UseBackColor = true;
            simpleButton1.Appearance.BackColor = Color.FromArgb(0, 120, 215);
            simpleButton1.Appearance.ForeColor = Color.White;
            simpleButton1.ToolTip = "新增";


            // 表格美化
            gridView1.OptionsView.EnableAppearanceEvenRow = true;
            gridView1.Appearance.EvenRow.BackColor = Color.FromArgb(245, 249, 255);
            gridView1.Appearance.HeaderPanel.Font = new Font("微软雅黑", 11, FontStyle.Bold);
            gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gridView1.Appearance.Row.Font = new Font("微软雅黑", 10);
            gridView1.OptionsView.ShowGroupPanel = false;
            // gridView1.OptionsBehavior.Editable = false; // 注释掉此行
            gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView1.OptionsSelection.EnableAppearanceFocusedRow = true;
            gridView1.Appearance.FocusedRow.BackColor = Color.FromArgb(200, 230, 255);
            gridView1.Appearance.FocusedRow.ForeColor = Color.Black;
            gridView1.Appearance.SelectedRow.BackColor = Color.FromArgb(0, 120, 215);
            gridView1.Appearance.SelectedRow.ForeColor = Color.White;
            await ReInit();
        }

        private async Task<List<PACKINGMODEL>> GetPackingListAsync()
        {
            using(HttpClient client = new HttpClient())
            {
                var response = await HttpClientHelper.RequestUrl(RequestType.Get, ProductionGolab.GetPackingList, null);
                var result = JsonConvert.DeserializeObject<ApiPaging<List<PACKINGMODEL>>>(response);
                // 设置总记录数
                paginationControl1.TotalRecords = result.TotalCount;

                
                return result.Data;
            }
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            AddPackingForm addPackingForm = new AddPackingForm();
            var result = addPackingForm.ShowDialog();
            if(result==DialogResult.OK)
            {
                // 重新加载数据
                await ReInit();
            }
        }

        private void paginationControl1_Load(object sender, EventArgs e)
        {
        }
        private async Task LoadData(int pageIndex, int pageSize)
        {
            // 计算起始索引
            int startIndex = (pageIndex - 1) * pageSize;

            // 示例：从数据库获取数据
            // 使用SQL语句如：SELECT * FROM Table ORDER BY ID OFFSET {startIndex} ROWS FETCH NEXT {pageSize} ROWS ONLY

            // 或者从内存中筛选数据
            var packingList = await GetPackingListAsync();
            var pageData = packingList.Skip(startIndex).Take(pageSize).ToList();

            // 绑定数据到控件
            gridControl1.DataSource = pageData;
        }

        private async void paginationControl1_PageChanged(object sender, PageChangedEventArgs e)
        {
            // e.CurrentPage: 当前页码
            // e.PageSize: 每页记录数
            await LoadData(e.CurrentPage, e.PageSize);
        }

        private async void paginationControl1_PageSizeChanged(object sender, PageSizeChangedEventArgs e)
        {
            // e.CurrentPage: 当前页码
            // e.PageSize: 每页记录数
            await LoadData(e.CurrentPage, e.PageSize);
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

        }
    }
}